<?php

// Error handler function for throw errors as exceptions
function exception_error_handler ($number, $string, $file, $line)
{
	throw new ErrorException($string, 0, $number, $file, $line);
}

// Define error handling
set_error_handler('exception_error_handler');
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', '1');

define('BASEDIR', dirname(dirname(__FILE__)));

include BASEDIR . '/app/web_config.php';
include $config['lqdir'] . '/lib.php';

// Set ini settings from the config file (mostly session stufF)
foreach ($config['ini_vars'] as $name => $value)
{
	ini_set($name, $value);
}

$output = array();
$sessid = false;
$sessname = session_name();

includelq('Input');
$input = new lqInput();

// Try to determine session id
$input->accept('POST', 'GET', 'COOKIE');
if (isset($input[$sessname]))
{
	$sessid = $input[$sessname];
	session_id($sessid);
}

session_start();

// If we didn't determine a session ID, generate a new one (if taken)
if ($sessid === false && count($_SESSION) !== 0)
{
	session_regenerate_id();
	foreach ($_SESSION as $key => $value)
	{
		unset($_SESSION[$key]);
	}
}

$input->accept('POST', 'GET');
$clitext = strtolower($input['input']);

// If we have no input, don't perform any unneccessary operations
if (strlen($clitext) < 1)
{
	$wcc = null;
	return;
}

// If we have no saved session data, create empty arrays
if (!isset($_SESSION['clidata']) || !is_array($_SESSION['clidata']) ||
	$clitext == '/reset')
{
	$_SESSION['clidata'] = array();
}
if (!isset($_SESSION['history']) || !is_array($_SESSION['history']))
{
	$_SESSION['history'] = array();
}

// If the line is not repeat of previous, add it to history
if (count($_SESSION['history']) == 0 || $clitext != $_SESSION['history'][0])
{
	array_unshift($_SESSION['history'], $clitext);
}

require dirname(__FILE__) . '/../lib/lib.php';

// Load the mode used by the calculator
$mode = isset($_SESSION['mode']) ? $_SESSION['mode'] : 'std';

try
{
	$climode = new CliMode($mode);
}
catch (CliModeException $ex)
{
	$output[] = "Unable to load mode '$_SESSION[mode]', reverting to std library";
	$climode = new CliMode('std');
	unset($_SESSION['mode']);
}

$climode->loadLibrary('web');

// Load the context
$context = new CliContext($climode);

try
{
	$context->importData($_SESSION['clidata']);
}
catch (CliDataException $ex)
{
	$output[] = "CliCalc data corruption detected, use /reset to reset.\n\n";
	$output[] = $ex->getTraceAsString();
	return;
}

if ($errors = $context->getImportErrors())
{
	$output[] = "Some variables were discarded due to errors in data:\n";

	foreach ($errors as $i => $error)
	{
		$output[] = $i . ". " . $error->getMessage() . "\n";
	}
}

// Load the calculator itself
$calc = new CliCalc($context);
$calc->loadInput($clitext);
$calc->runInput();

$output = array_merge($output, $calc->getOutput());

// Export the context data
$_SESSION['clidata'] = $context->exportData();
$_SESSION['history'] = array_slice($_SESSION['history'], 0, $context->getSetting('maxhistory'));

?>